package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.service.ShoppingCarService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    ShoppingCarMapper shoppingCarMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetMealMapper setMealMapper;

    /**
     * 购物车添加方法
     *
     * @param shoppingCartDTO
     * @return
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
//        1.判断购物车是否有该产品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
//        用户获取id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
//        使用菜品id、套餐id和用户的id进行查询
        List<ShoppingCart> list = shoppingCarMapper.selectWithDishAndCategory(shoppingCart);
//        2.有该产品为添加数量，update就行
        if (list.size() > 0 && list != null) {
//            因为购物车不可能存在重复的，只会有一个元素
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
//            进行数据的更新就行
            shoppingCarMapper.update(shoppingCart1);
        } else {
//            3.无该产品进行添加数据就行，insert
//            判断是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //            获得该产品的其他信息
                Dish dish = dishMapper.selectById(dishId);
//                价格
                shoppingCart.setAmount(dish.getPrice());
//                图片
                shoppingCart.setImage(dish.getImage());
//                名字
                shoppingCart.setName(dish.getName());
            } else {
                Long setmealId = shoppingCart.getSetmealId();
                SetmealVO setmealVO = setMealMapper.selectByID(setmealId);
                //                价格
                shoppingCart.setAmount(setmealVO.getPrice());
//                图片
                shoppingCart.setImage(setmealVO.getImage());
//                名字
                shoppingCart.setName(setmealVO.getName());
            }
//            共有
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
//            进行数据的插入
            shoppingCarMapper.insert(shoppingCart);


        }

    }

    /**
     * 购物车查看
     *
     * @return
     */
    public List<ShoppingCart> select() {
//        获取userId
        Long currentId = BaseContext.getCurrentId();
//        根据id去查数据
       ShoppingCart s =  ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list = shoppingCarMapper.selectWithDishAndCategory(s);
        return list;
    }



    /**
     * 清空购物车
     * @return
     */
    public void deleteAll() {
//        获取用户的id
        Long currentId = BaseContext.getCurrentId();
//        根据id删除数据库的数据
        shoppingCarMapper.deleteAll(currentId);
    }

    /**
     * 减少购物车的份数
     * @return
     */
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
//        首先肯定先获取对应的用户id
        Long currentId = BaseContext.getCurrentId();
//        构造对应的对象，封装数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(currentId);
//        查询对象数据所对应的数据库对象
        List<ShoppingCart> list = shoppingCarMapper.selectWithDishAndCategory(shoppingCart);
        ShoppingCart shoppingCart1 = list.get(0);
//        需要判断份数
//        1.大于0的情况
        int i = shoppingCart1.getNumber() - 1;
        if (i > 0){
            shoppingCart1.setNumber(i);
            shoppingCarMapper.update(shoppingCart1);
        }else {
//            已经没有了，就需要删除数据库对应的数据
            shoppingCarMapper.deleteOne(shoppingCart1);
        }
    }

}
